package com.project.service.impl;

import com.project.bean.CarBean;
import com.project.bean.ManBean;
import com.project.mapper.IManMapper;
import com.project.service.IManService;
import org.apache.ibatis.session.SqlSession;
import java.util.List;

public class ManServiceImpl extends BaseService implements IManService {
    @Override
    public List<ManBean> findAll() {
        SqlSession session = this.getSession();
        IManMapper mapper = session.getMapper(IManMapper.class);
        List<ManBean> list = mapper.findAll();
        session.close();
        return list;
    }

    @Override
    public void addMan(ManBean man, List<CarBean> carList) {
        SqlSession session = this.getSession();
        IManMapper mapper = session.getMapper(IManMapper.class);
        mapper.addMan(man);
        mapper.addCar(carList, man.getId());
        session.commit();
        session.close();
    }

    @Override
    public void delSetNull(int id) {
        SqlSession session = this.getSession();
        IManMapper mapper = session.getMapper(IManMapper.class);
        mapper.delSetNull(id);
        session.commit();
        session.close();
    }

    @Override
    public ManBean findByIdIncludeCar(int id) {
        SqlSession session = this.getSession();
        IManMapper mapper = session.getMapper(IManMapper.class);
        ManBean man = mapper.findByIdIncludeCar(id);
        session.close();
        return man;
    }


    public static void main(String[] args) {
        IManService service = new ManServiceImpl();
//        System.out.println(service.findAll());
//        service.addMan(new ManBean("aaa", "222"), Arrays.asList(new CarBean("丰田",120000),
//                new CarBean("本田",80000)));
//        service.delSetNull(6);
        System.out.println(service.findByIdIncludeCar(1));
    }
}
